package com.cardbaobao.cardbabyclient.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.api.Api;
import com.cardbaobao.cardbabyclient.base.Constant;
import com.cardbaobao.cardbabyclient.buried.Self;
import com.cardbaobao.cardbabyclient.model.QuickDebitFeeRateData;
import com.cardbaobao.cardbabyclient.model.result.QuickDebitFeeRateResult;
import com.cardbaobao.cardbabyclient.ui.activity.base.BaseActivity;
import com.cardbaobao.cardbabyclient.utils.ApplicationHelper;
import com.cardbaobao.cardbabyclient.buried.GoogleAnalyticsUtil;
import com.cardbaobao.cardbabyclient.utils.JsonUtil;
import com.cardbaobao.cardbabyclient.utils.ToastUtil;
import com.cardbaobao.cardbabyclient.widget.TitleBar;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Subscriber;

/**
 * Created by Jiangzx on 2017/9/12 17:17.
 */

public class QuickDebitResultActivity extends BaseActivity {
    public static final String DEBIT_RESULT_CODE_I="code";
    @BindView(R.id.title_bar)
    TitleBar titleBar;
    @BindView(R.id.iv_debit_result)
    ImageView ivDebitResult;
    @BindView(R.id.tv_arrival_time)
    TextView tvArrivalTime;
    @BindView(R.id.tv_debit_result)
    TextView tvDebitResult;

    String orderNo;
    int code;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_quick_debit_result;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        titleBar.setLeftViewOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        Intent intent=getIntent();

        mCurrentPageId= Self.Page.app_quick_receivables_result;

        code=intent.getIntExtra(DEBIT_RESULT_CODE_I,0);
        orderNo=intent.getStringExtra(QuickDebitShowActivity.ORDER_NO_S);
        if(code== Constant.SUC_CODE){
            ivDebitResult.setImageResource(R.mipmap.img_debit_success);
            tvDebitResult.setText("您已成功收款");
            tvArrivalTime.setVisibility(View.VISIBLE);
            tvDebitResult.setTextColor(0XFF222222);
        }else{
            ivDebitResult.setImageResource(R.mipmap.img_debit_failure);
            tvDebitResult.setText("交易失败");
            tvArrivalTime.setVisibility(View.GONE);
            tvDebitResult.setTextColor(0XFF666666);
        }
    }

    private void getFeeRates(){
        showProgressDialog(false);
        Api.getQuickDebitFeeRate().subscribe(new Subscriber<String>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable throwable) {
                dismissProgressDialog();
                ToastUtil.showToast(ApplicationHelper.getApplicationContext().
                        getString(R.string.string_request_data_fail));
            }

            @Override
            public void onNext(String s) {
                dismissProgressDialog();
                QuickDebitFeeRateResult result= JsonUtil.json2Object(s,QuickDebitFeeRateResult.class);
                if(result.getCode()== Constant.SUC_CODE){
                    QuickDebitFeeRateData data=result.getData();
                    if(data!=null){
                        ArrayList<QuickDebitFeeRateData.FeeRate> rates=data.getRate_list();
                        if(rates!=null&&rates.size()>0){
                            boolean canQuickDebit=false;
                            for(QuickDebitFeeRateData.FeeRate r:rates){
                                if(Constant.KQB010101.equals(r.getChannel_type())){
                                    canQuickDebit=true;
                                    break;
                                }
                            }
                            if(canQuickDebit){
                                Intent intent = new Intent();
                                intent.putParcelableArrayListExtra(FeeRateActivity.QUICK_DEBIT_FEE_RATE_LIST_ARRAY,
                                        rates);
                                intent.putExtra(FeeRateActivity.FEE_TYPE_I,
                                        FeeRateActivity.QUICK_DEBIT_FEE_TYPE);
                                intent.putExtra(PRE_PAGE_ID_S,
                                        mCurrentPageId);
                                intent.setClass(mContext, QuickDebitActivity.class);
                                startActivity(intent);
                            }else{
                                ToastUtil.showToast(ApplicationHelper.getApplicationContext().
                                        getString(R.string.string_data_exception));
                            }
                        }
                    }
                }else{
                    ToastUtil.showToast(ApplicationHelper.getApplicationContext().
                            getString(R.string.string_request_data_fail));
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(code== Constant.SUC_CODE){
            GoogleAnalyticsUtil.setScreenName("快捷收款成功");
        }else{
            GoogleAnalyticsUtil.setScreenName("快捷收款失败");
        }
    }

    @OnClick({R.id.tv_view_debit_detail,R.id.bt_go_back,R.id.bt_debit})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.tv_view_debit_detail:
                //查看交易详情
                Intent debitDetailIntent=new Intent();
                debitDetailIntent.setClass(QuickDebitResultActivity.this,
                        TransactionDetailActivity.class);
                debitDetailIntent.putExtra(TransactionDetailActivity.ORDER_NUMBER_S,
                        orderNo);
                debitDetailIntent.putExtra(TransactionDetailActivity.DETAIL_TYPE_I,
                        TransactionDetailActivity.TYPE_DEBIT);
                startActivity(debitDetailIntent);
                break;
            case R.id.bt_go_back:
                //返回
                finish();
                break;
            case R.id.bt_debit:
                //继续收款
                getFeeRates();
                break;
        }
    }
}
